package lanou.giftsay_01.fragment.main;

import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import lanou.giftsay_01.fragment.categorybody.GonglueFragment;
import lanou.giftsay_01.adapter.CgAdapter;
import lanou.giftsay_01.fragment.categorybody.DanPinFragment;
import lanou.giftsay_01.main.categorymain.SelectGiftActivity;
import lanou.giftsay_01.main.guidemain.SearchMainActivity;
import lanou.giftsay_01.tools.BaseFragment;
import lanou.giftsay_01.R;

/**
 * Created by dllo on 16/8/15.
 */
public class CategoryFragment extends BaseFragment {

    private ViewPager vp;
    private TabLayout tb;
    private CgAdapter cgAdapter;
    private ArrayList<Fragment> arrayList;
    private ArrayList<String> title;
    private TextView tv;
    private LinearLayout ll;

    @Override
    protected int setLayout() {
        return R.layout.category_fragment;
    }

    @Override
    protected void initView() {
        vp = (ViewPager) getView().findViewById(R.id.vp_category_fragment);
        ll = (LinearLayout) getView().findViewById(R.id.ll_category);
        tb = (TabLayout) getView().findViewById(R.id.tb_category_fragment);
        tv = (TextView) getView().findViewById(R.id.tv_category_gift);
        cgAdapter = new CgAdapter(getChildFragmentManager());
    }

    @Override
    protected void initData() {
        arrayList = new ArrayList<>();
        title = new ArrayList<>();
        title.add("攻略");
        title.add("单品");
        arrayList.add(new GonglueFragment());
        arrayList.add(new DanPinFragment());
        cgAdapter.setTitle(title);
        cgAdapter.setArrayList(arrayList);
        vp.setAdapter(cgAdapter);
        tb.setupWithViewPager(vp);
        tb.setTabTextColors(Color.WHITE ,Color.WHITE);
        tb.setSelectedTabIndicatorColor(Color.WHITE);
        /**
         * "选礼神器"设置监听跳转
         */
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CategoryFragment.this.getContext(),SelectGiftActivity.class);
                startActivity(intent);
            }
        });
        /**
         * 给TabLayout设置监听
         * 控制"选礼神器"的隐藏和显示
         */

        tb.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                vp.setCurrentItem(tab.getPosition());
               if(tab.getPosition()==1){
                   tv.setVisibility(View.VISIBLE);
               }else if(tab.getPosition()==0){
                   tv.setVisibility(View.INVISIBLE);
               }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        /**
         * 给linearlayout设置监听跳转
         */

        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CategoryFragment.this.getContext(), SearchMainActivity.class);
                startActivity(intent);

            }
        });
    }

}
